import { defHttp } from '/@/utils/http/axios';
import { getMenus } from '/@/utils/menu'
import { ErrorMessageMode } from '/#/axios';
import {
  GetUserInfoModel,
  LoginParams,
  LoginResultModel,
  PermissionResultModel,
} from './model';

enum Api {
  Login = '/system/login',
  Logout = '/system/logout',
  GetUserInfo = '/oauth/user-info',
  GetPermCode = '/system/authorize/permissions',
  GetUserMenuTree = '/system/menu/tree',
  TestRetry = '/testRetry',
  OA = '/oa/message/list',
  OANews = '/oa/news/box',
  Read = '/oa/news/read',
  workReadAll = '/oa/message/read-all-message',
  schemaReadAll = '/oa/message/read-all-schedule-message',
  Single = '/oa/message/read-message',
  QrLogin = '/system/qrcode-login',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return defHttp.get<PermissionResultModel>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.post({ url: Api.Logout });
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}

/**
 * @description: Get user menu based on id
 */

export const getMenuList = () => {
  // console.log(JSON.parse(localStorage.getItem('menuPermissionList')),"1111111")
  return JSON.parse(localStorage.getItem('menuPermissionList'))
  // return getMenus()
};

export function getTreeName(list,id){
  for (let i = 0; i < list.length; i++) {
    if(list[i].id===id){
      return list[i]
    }else if(list[i].children && list[i].children.length>0){
      let res=getTreeName(list[i].children,id)
      if(res){
        return res
      }
    }
  }
}

export function getOaMessage() {
  return defHttp.get({ url: Api.OA });
}

export function getOaNews(type, mode: ErrorMessageMode = 'modal') {
  // return defHttp.get(
  //   { url: Api.OANews, params: { limit: 1, size: 10, type } },
  //   {
  //     errorMessageMode: mode,
  //   },
  // );
  return []
}
export function setOaRead(id, mode: ErrorMessageMode = 'modal') {
  return defHttp.put(
    { url: Api.Read, params: id },
    {
      errorMessageMode: mode,
    },
  );
}

export function setWorkReadAll(mode: ErrorMessageMode = 'modal') {
  return defHttp.put(
    { url: Api.workReadAll },
    {
      errorMessageMode: mode,
    },
  );
}

export function setSchemaReadAll(mode: ErrorMessageMode = 'modal') {
  return defHttp.put(
    { url: Api.schemaReadAll },
    {
      errorMessageMode: mode,
    },
  );
}

export function setSingleRead(id, mode: ErrorMessageMode = 'modal') {
  return defHttp.put(
    { url: Api.Single, params: { id } },
    {
      errorMessageMode: mode,
    },
  );
}

export function getOauthAuthorizeUrl(source: string, mode: ErrorMessageMode = 'modal') {
  return defHttp.post(
    { url: Api.QrLogin, params: { source } },
    {
      errorMessageMode: mode,
    },
  );
}
